package com.nw.common;

import com.alibaba.fastjson.JSON;
import com.nw.enums.ReturnStatusEnum;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Auther: Administrator
 * @Date: 2018/8/10 0010 14:27
 * @Description:
 */
public class BaseController {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(BaseController.class);
    @ExceptionHandler
    public void exp(HttpServletRequest request, HttpServletResponse response, Exception ex) throws IOException {
        Result result = new Result();
        result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
        result.setStatus(ReturnStatusEnum.FAILURE.getCode());
        result.setReturnMsg(ReturnStatusEnum.EXCEPTION.getDesc());
        result.setData(ex.getMessage());
        LOGGER.error("异常：",ex);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter pw = response.getWriter();

        pw.print(JSON.toJSONString(result));
        pw.flush();
        pw.close();
    }
}
